package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.MainDataInfoMainDataInfoActivityAdpater;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;

import com.google.gson.reflect.TypeToken;

public class MainDataInfoActivity extends BaseActivity {
	private TextView tv_lishixiangqingbiaoti;
	private TextView tv_lishixiangqingleixing;
	private TextView tv_lishixiangqingshijian;
	private TextView tv_lishixiangqinggaishu;
	private List<Map<String,String>> goods;
	private ListView iv_lishitupian;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_center_lishixiangqing);
		intiview();
		
	}

	private void intiview() {
		goods = new ArrayList<Map<String,String>>();
		tv_lishixiangqingbiaoti = (TextView) findViewById(R.id.tv_lishixiangqingbiaoti);
		tv_lishixiangqingleixing = (TextView) findViewById(R.id.tv_lishixiangqingleixing);
		tv_lishixiangqingshijian = (TextView) findViewById(R.id.tv_lishixiangqingshijian);
		tv_lishixiangqinggaishu = (TextView) findViewById(R.id.tv_lishixiangqinggaishu);
		iv_lishitupian = (ListView) findViewById(R.id.iv_lishitupian);
		getdata();
		data();
		initTitlBar();
	}
	
	
	
	private Handler Handler = new ServerHandler() {
		private List<Map<String,String>> imagelist;
		private MainDataInfoMainDataInfoActivityAdpater postAdapter;

		@Override
		public void onSuccess(Bundle data, String state, String msg) {

		
			String dataStr = data.getString(HttpCons.DATA);
			if (state.equals("001")) {
				Map<String,Object> tuu =  HttpServer.gson.fromJson(dataStr,
						new TypeToken<Map<String,Object>>() {
						}.getType());
				if (tuu!=null) {
					Log.e("XX", tuu.get("title")+"");
					tv_lishixiangqingbiaoti.setText(tuu.get("title").toString());
					tv_lishixiangqingleixing.setText(tuu.get("typename").toString());
					tv_lishixiangqingshijian.setText(tuu.get("businesstime").toString());
					tv_lishixiangqinggaishu.setText(tuu.get("content").toString());
					imagelist = (List<Map<String,String>>)tuu.get("Imagelist");
					
					goods.addAll(imagelist);
					postAdapter = new MainDataInfoMainDataInfoActivityAdpater(
							imagelist,MainDataInfoActivity.this);
					iv_lishitupian.setAdapter(postAdapter);
				}
			
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
		
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(MainDataInfoActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	
	
	
	public void initTitlBar() {
		tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_title = (TextView) findViewById(R.id.tv_title_title);
	
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		tv_title_title.setText(getString(R.string.idop));
		
	}
	private String seq;
	private TextView tv_title_back;
	private TextView tv_title_title;
	private void getdata(){
	seq = getIntent().getStringExtra("seq");
	}
	private void data(){
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("relationid", seq);
		map.put("functionId", 1);
		map.put("seq", seq);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.QueryHistoricalDataForDetails, parameter, Handler);

	}
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_title_back:
			finish();
			break;

		default:
			break;
		}
	}

}
